from django.shortcuts import render, redirect
from .models import *

# Create your views here.
def to_index(request):
    return render(request, "index.html")

def to_basic_introduction(request):
    basic_intros = BasicIntro.objects.all()
    return render(request, "basic_introduction.html", {'basic_intros':basic_intros})

def to_structure_introduction(request):
    structure_intros = StructureIntro.objects.all()
    return render(request, "structure_introduction.html", {'structure_intros':structure_intros})

def to_field_type_introduction(request):
    field_type_intros = FieldTypeIntro.objects.all()
    return render(request, "field_type_introduction.html", {'field_type_intros':field_type_intros})

def to_table_demo(request):
    table_demos = TableDemo.objects.all()
    return render(request, "table_demo.html", {'demo_items':table_demos})

def table_demo_insert(request):
    integer_get = request.POST.get('integer')
    boolean_get = request.POST.get('boolean')
    date_get = request.POST.get('date')
    char_get = request.POST.get('char')
    text_get = request.POST.get('text')
    print(str(integer_get) + "-" + str(boolean_get) + "-" + str(date_get) + "-" + str(char_get) + "-" + str(text_get))
    new_integer = int(integer_get)
    new_boolean = bool(boolean_get)
    new_date = date_get
    new_char = char_get
    new_text = text_get
    new_demo = TableDemo(integer=new_integer, boolean=new_boolean, date=new_date, char=new_char, text=new_text)
    new_demo.save()
    return redirect('/mysql_demo/table_demo/')

def to_student_demo(request):
    students = Student.objects.all()
    return render(request, "student_demo.html", {'students':students})

def student_demo_insert(request):
    stu_id_get = request.POST.get('stu_id')
    name_get = request.POST.get('name')
    gender_get = request.POST.get('gender')
    major_get = request.POST.get('major')
    print(str(stu_id_get) + "-" + str(name_get) + "-" + str(gender_get) + "-" + str(major_get))
    new_stu_id = stu_id_get
    new_name = name_get
    new_gender = gender_get
    new_major = major_get
    new_student = Student(stu_id=new_stu_id, name=new_name, gender=new_gender, major=new_major)
    new_student.save()
    return redirect('/mysql_demo/student_demo/')